import * as vscode from 'vscode';

export class SidebarProvider implements vscode.TreeDataProvider<SidebarItem> {
    private _onDidChangeTreeData: vscode.EventEmitter<SidebarItem | undefined | null | void> = new vscode.EventEmitter<SidebarItem | undefined | null | void>();
    readonly onDidChangeTreeData: vscode.Event<SidebarItem | undefined | null | void> = this._onDidChangeTreeData.event;

    constructor() {}

    refresh(): void {
        this._onDidChangeTreeData.fire();
    }

    getTreeItem(element: SidebarItem): vscode.TreeItem {
        return element;
    }

    getChildren(element?: SidebarItem): Thenable<SidebarItem[]> {
        if (!element) {
            // 根节点 - 显示所有功能分类
            return Promise.resolve([
                new SidebarItem(
                    '🚀 代码生成',
                    'code-generation',
                    vscode.TreeItemCollapsibleState.Expanded
                ),
                new SidebarItem(
                    '🧪 测试生成',
                    'test-generation',
                    vscode.TreeItemCollapsibleState.Expanded
                ),
                new SidebarItem(
                    '🔧 工具',
                    'tools',
                    vscode.TreeItemCollapsibleState.Expanded
                )
            ]);
        } else {
            // 子节点 - 具体功能
            return Promise.resolve(this.getChildItems(element));
        }
    }

    private getChildItems(element: SidebarItem): SidebarItem[] {
        switch (element.contextValue) {
            case 'code-generation':
                return [
                    new SidebarItem(
                        '✨ 智能代码续写',
                        'action',
                        vscode.TreeItemCollapsibleState.None,
                        'oh-yidian-tong.completeCode'
                    ),
                    new SidebarItem(
                        '🎨 自动生成代码',
                        'action',
                        vscode.TreeItemCollapsibleState.None,
                        'oh-yidian-tong.generateCode'
                    )
                ];
            case 'test-generation':
                return [
                    new SidebarItem(
                        '📝 生成单元测试',
                        'action',
                        vscode.TreeItemCollapsibleState.None,
                        'oh-yidian-tong.generateUnitTest'
                    ),
                    new SidebarItem(
                        '🔬 生成Fuzz测试',
                        'action',
                        vscode.TreeItemCollapsibleState.None,
                        'oh-yidian-tong.generateFuzzTest'
                    )
                ];
            case 'tools':
                return [
                    new SidebarItem(
                        '🤖 Agent 对话',
                        'action',
                        vscode.TreeItemCollapsibleState.None,
                        'oh-yidian-tong.openAgentChat'
                    ),
                    new SidebarItem(
                        '📦 修改GN文件',
                        'action',
                        vscode.TreeItemCollapsibleState.None,
                        'oh-yidian-tong.modifyGnFile'
                    ),
                    new SidebarItem(
                        '🔍 代码分析',
                        'action',
                        vscode.TreeItemCollapsibleState.None,
                        'oh-yidian-tong.analyzeCode'
                    ),
                    new SidebarItem(
                        '⚙️ 配置设置',
                        'action',
                        vscode.TreeItemCollapsibleState.None,
                        'oh-yidian-tong.configureSettings'
                    )
                ];
            default:
                return [];
        }
    }
}

class SidebarItem extends vscode.TreeItem {
    constructor(
        public readonly label: string,
        public readonly contextValue: string,
        public readonly collapsibleState: vscode.TreeItemCollapsibleState,
        commandId?: string
    ) {
        super(label, collapsibleState);
        
        this.contextValue = contextValue;
        
        // 如果是可执行的操作，添加命令
        if (commandId) {
            this.command = {
                command: commandId,
                title: label,
                arguments: []
            };
            
            // 添加描述
            this.tooltip = this.getTooltip(commandId);
        }
    }

    private getTooltip(command: string): string {
        const tooltips: { [key: string]: string } = {
            'oh-yidian-tong.completeCode': '根据当前代码上下文，智能续写代码',
            'oh-yidian-tong.generateCode': '根据提示自动生成代码',
            'oh-yidian-tong.generateUnitTest': '为选中的代码生成单元测试',
            'oh-yidian-tong.generateFuzzTest': '生成Fuzz测试代码',
            'oh-yidian-tong.openAgentChat': '打开Agent对话视图，进行对话式开发',
            'oh-yidian-tong.modifyGnFile': '修改GN构建文件',
            'oh-yidian-tong.analyzeCode': '分析代码质量和潜在问题',
            'oh-yidian-tong.configureSettings': '打开插件配置设置'
        };
        return tooltips[command] || '';
    }

    iconPath = this.getIcon();

    private getIcon(): vscode.ThemeIcon | undefined {
        switch (this.contextValue) {
            case 'code-generation':
                return new vscode.ThemeIcon('rocket');
            case 'test-generation':
                return new vscode.ThemeIcon('beaker');
            case 'tools':
                return new vscode.ThemeIcon('tools');
            case 'action':
                return new vscode.ThemeIcon('play');
            default:
                return undefined;
        }
    }
}

